package com.dhcc.DRGsHosp.infra.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @title :DataSourceUtil.java
 * @description :获取数据库连接信息
 * @author :liqingyang
 * @date :2015年7月25日
 * @version :2.0.0
 * @copyright :DHCC 2013-2015
 */
public class DataSourceUtil {

	private String dbUrl;
	private String dbUserName;
	private String dbPassWord;
	private String dbType;
	private String dataPath;
	

	// 注意，这里没有final
	private static DataSourceUtil dataSourceUtil = null;

	// 静态工厂方法
	public static DataSourceUtil getInstance() {
		
		if (dataSourceUtil == null) {
			InputStream propInputStream = DataSourceUtil.class
					.getResourceAsStream("/drgsys.properties");
			Properties properties = new Properties();
			try {
				properties.load(propInputStream);
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				if(propInputStream!=null) {
					try {
						propInputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			String dbUrl = properties.getProperty("drgurl");
			dbUrl = dbUrl.substring(dbUrl.lastIndexOf("@")+1,dbUrl.length());
			String dbUserName = properties.getProperty("koalauser");
			String dbPassWord = properties.getProperty("koalapassword");
			String dbType = properties.getProperty("database.Type");
			String dataPath=DataSourceUtil.class.getResource("/").getFile().toString();  
			dataPath = dataPath.substring(1,dataPath.lastIndexOf("WEB-INF"))+"data";
			dataSourceUtil = new DataSourceUtil(dbUrl, dbUserName, dbPassWord,dbType,dataPath);
		}
		return dataSourceUtil;
	}

	public DataSourceUtil(String dbUrl, String dbUserName, String dbPassWord,String dbType,String dataPath) {
		this.dbUrl = dbUrl;
		this.dbUserName = dbUserName;
		this.dbPassWord = dbPassWord;
		this.dbType = dbType;
		this.dataPath = dataPath;
	}

	public String getDbUrl() {
		return dbUrl;
	}

	public void setDbUrl(String dbUrl) {
		this.dbUrl = dbUrl;
	}

	public String getDbUserName() {
		return dbUserName;
	}

	public void setDbUserName(String dbUserName) {
		this.dbUserName = dbUserName;
	}

	public String getDbPassWord() {
		return dbPassWord;
	}

	public void setDbPassWord(String dbPassWord) {
		this.dbPassWord = dbPassWord;
	}

	public String getDbType() {
		return dbType;
	}

	public void setDbType(String dbType) {
		this.dbType = dbType;
	}

	public String getDataPath() {
		return dataPath;
	}

	public void setDataPath(String dataPath) {
		this.dataPath = dataPath;
	}

}
